http=require('http'),

config=require('./config.js');
var timers={};		// 任务记时器列表
// 测试自动开奖
// 读取 config.js 里面的玩法接口,获取彩票数据
    runTask();
function runTask(){
    if(config.cp.length) config.cp.forEach(function(conf){
        timers[conf.name]={};
        timers[conf.name][conf.timer]={timer:null, option:conf};
        try{
            if(conf.enable) run(conf);
        }catch(err){
            // 如果采集失败，重新开始采集

            restartTask(conf, config.errorSleepTime);
        }
    });
}

function restartTask(conf, sleep, flag){
    if(sleep<=0) sleep=config.errorSleepTime;
    if(!timers[conf.name]) timers[conf.name]={};
    if(!timers[conf.name][conf.timer]) timers[conf.name][conf.timer]={timer:null,option:conf};

    if(flag){

        var opt;
        for(var t in timers[conf.name]){
            opt=timers[conf.name][t].option;
            clearTimeout(timers[opt.name][opt.timer].timer);
            timers[opt.name][opt.timer].timer=setTimeout(run, sleep*1000, opt);
            log('休眠'+sleep+'秒后从'+opt.source+'采集'+opt.title+'数据...');
        }
    }else{

        clearTimeout(timers[conf.name][conf.timer].timer);
        timers[conf.name][conf.timer].timer=setTimeout(run, sleep*1000, conf);
        log('休眠'+sleep+'秒后从'+conf.source+'采集'+conf.title+'数据...');
    }
}

function run(conf){
    if(timers[conf.name][conf.timer].timer) clearTimeout(timers[conf.name][conf.timer].timer);
    log('开始从'+conf.source+'采集'+conf.title+'数据');
    var option=JSON.parse(JSON.stringify(conf.option));
    //option.path+='?'+(new Date()).getTime();
    http.request(option, function(res){
        var data="";
        res.on("data", function(_data){
            data+=_data.toString();
        });
        res.on("end", function(){
            try{
                try{

                    data=conf.parse(data);
                    //return {
                    //                         type:51,
                    //                         time:m[3],
                    //                         number:m[1],
                    //                         data:m[2]
                    //                     };



                    var data2 = encodeURI(JSON.stringify(data));
                    var options={
                        host:"bocai.666it.com",
                        timeout:50000,
                        path: '/2.php?data='+data2,
                        headers:{
                            "User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) "
                        }
                    }
                    http.request(options, function(res){
                        var data="";
                        res.on("data", function(_data){
                            data+=_data.toString();
                        });

                        res.on("end", function(){
                            try{
                                console.log(data)
                            }catch(err){
                                throw('解析'+conf.title+'数据出错：'+err);
                            }
                        });

                    }).end();

                }catch(err){
                    throw('解析'+conf.title+'数据出错：'+err);
                }
            }catch(err){

                // log('运行出错：%s，休眠%f秒'.format(err, config.errorSleepTime));
                restartTask(conf, config.errorSleepTime);
            }
        });

        res.on("error", function(err){
            log(err);

            restartTask(conf, config.errorSleepTime);
        });

    }).on('timeout', function(err){
        log('从'+conf.source+'采集'+conf.title+'数据超时');

        restartTask(conf, config.errorSleepTime);
    }).on("error", function(err){
        // 一般网络出问题会引起这个错
        log(err);

        restartTask(conf, config.errorSleepTime);

    }).end();

}